import { fileURLToPath, URL } from 'node:url';

import { defineConfig } from 'vite';
import vue from '@vitejs/plugin-vue';
import vueJsx from '@vitejs/plugin-vue-jsx';
import legacy from '@vitejs/plugin-legacy';
import { createHtmlPlugin } from 'vite-plugin-html';
import dotenv from 'dotenv';

// 加载环境变量
dotenv.config({ path: `.env` });

//@ts-ignore
import tffConfig from './plugins/vite-plugin-config-runtime.mjs';
// const viteAppCode = process.env.VITE_APP_CODE;

// https://vitejs.dev/config/
export default defineConfig({
  server:{
    host:'0.0.0.0',
    allowedHosts:true,
    watch:{
      ignored: [ '**/node_modules/**', '**/dist/**','**/.upgradeTempDir/**' ]
    }
  },
  build:{
    outDir: `dist/`,
    cssTarget: [ 'chrome52' ],
    minify: "terser",
    terserOptions: {
      compress: {
        drop_debugger: true
      }
    },
    rollupOptions: {
      /**
       * 配置 rollup 打包生成的文件是否开启 hash 文件名在这里修改
       */
      output: {
        entryFileNames: '[name].js',
        chunkFileNames: '[name].js',
        assetFileNames: '[name].[ext]',
        globals: {
          'vue': 'Vue',
          'vue-router': 'VueRouter',
          'vue-i18n': 'VueI18n',
          '@teld/t-components': 'TComponents',
        },
        format: 'system'
      },
      external: (id) => {
        // 自定义逻辑，确定模块是否应被视为外部模块
        return /^vue/.test(id) || id === 'vue-router' || id === 'vue-i18n' || id === '@teld/t-components';
      }
    },
    // rollupOptions: {
    //   output: {
    //     entryFileNames: `[name].js`,
    //     chunkFileNames: `[name].js`,
    //     assetFileNames: `[name].[ext]`
    //   }
    // }
  },
  plugins: [
    tffConfig(),
    vue(),
    vueJsx(),
    legacy({
      targets: [ 'defaults', 'chrome < 87', 'edge < 88', 'safari < 14' ],
      polyfills: true,
      externalSystemJS: true,
    }),
    // quasar({
    //   sassVariables: 'src/quasar-variables.sass'
    // }),
    createHtmlPlugin({
      minify: false, 
      template: './index.html',
      inject: {
        data: {
          isPCModel:true,
          headerScript:`
              <script src="/config/app.setting.js" crossorigin="anonymous"></script>
              <script src="/echarts.min.js?_v=1" crossorigin="anonymous"></script>`,
          bodyScript: `
              <script src="/tff/api/common/teld-external.min.js"></script> 
              <script src="/tff/api/common/teld-plugin-fas-all.umd.js"></script> 
              <script src="/config/mockSgData.js"></script>
          `,
      
        }
      }
    }),
  ],
  resolve: {
    alias: {
      '@': fileURLToPath(new URL('./src', import.meta.url))
    }
  }
});
